home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / io / fileoutp.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  4.6 KB  |  151 lines

  1. /*
  2.  * @(#)FileOutputStream.java    1.17 95/08/10 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21. import java.io.File;
  22.  
  23. /**
  24.  * File output stream, can be constructed from
  25.  * a file descriptor or a file name.
  26.  * @see    FileInputStream
  27.  * @see    File
  28.  * @version     1.17, 08/10/95
  29.  * @author    Arthur van Hoff
  30.  */
  31. public
  32. class FileOutputStream extends OutputStream
  33. {
  34.     /**
  35.      * The system dependent file descriptor. The value is
  36.      * 1 more than actual file descriptor. This means that
  37.      * the default value 0 indicates that the file is not open.
  38.      */
  39.     private int fd;
  40.  
  41.     /**
  42.      * Creates an output file with the specified system dependent
  43.      * file name.
  44.      * @param name the system dependent file name 
  45.      * @exception IOException If the file is not found.
  46.      */
  47.     public FileOutputStream(String name) throws IOException {
  48.     SecurityManager security = System.getSecurityManager();
  49.     if (security != null) {
  50.         security.checkWrite(name);
  51.     }
  52.     open(name);
  53.     }
  54.     
  55.     /**
  56.      * Creates an output file with the specified system dependent
  57.      * file descriptor.
  58.      * @param fd the system dependent file descriptor
  59.      * @exception IOException If an I/O error has occurred.
  60.      */
  61.     public FileOutputStream(int fd) throws IOException {
  62.     SecurityManager security = System.getSecurityManager();
  63.     if (security != null) {
  64.         security.checkWrite(fd);
  65.     }
  66.     openfd(fd);
  67.     }
  68.     
  69.     /**
  70.      * Creates an output file with the specified File object.
  71.      * @param file the file to be opened for reading
  72.      * @exception IOException If the file is not found.
  73.      */
  74.     public FileOutputStream(File file) throws IOException {
  75.     this(file.getPath());
  76.     }
  77.     
  78.     /**
  79.      * Opens a file, with the specified name, for writing.
  80.      * @param name name of file to be opened
  81.      */
  82.     private native void open(String name) throws IOException;
  83.  
  84.     /**
  85.      * Uses a specified system dependent file descriptor for writing.
  86.      * @param fd the system dependent file descriptor
  87.      */
  88.     private native void openfd(int fd) throws IOException;
  89.  
  90.     /**
  91.      * Writes a byte of data. This method will block until the byte is 
  92.      * actually written.
  93.      * @param b the byte to be written
  94.      * @exception IOException If an I/O error has occurred.
  95.      */
  96.     public native void write(int b) throws IOException;
  97.  
  98.     /**
  99.      * Writes a sub array as a sequence of bytes.
  100.      * @param b the data to be written
  101.      * @param off the start offset in the data
  102.      * @param len the number of bytes that are written
  103.      * @exception IOException If an I/O error has occurred.
  104.      */
  105.     private native void writeBytes(byte b[], int off, int len) throws IOException;
  106.  
  107.     /**
  108.      * Writes an array of bytes. Will block until the bytes
  109.      * are actually written.
  110.      * @param b    the data to be written
  111.      * @exception IOException If an I/O error has occurred.
  112.      */
  113.     public void write(byte b[]) throws IOException {
  114.     writeBytes(b, 0, b.length);
  115.     }
  116.  
  117.     /**
  118.      * Writes a sub array of bytes. 
  119.      * @param b    the data to be written
  120.      * @param off    the start offset in the data
  121.      * @param len    the number of bytes that are written
  122.      * @exception IOException If an I/O error has occurred.
  123.      */
  124.     public void write(byte b[], int off, int len) throws IOException {
  125.     writeBytes(b, off, len);
  126.     }
  127.  
  128.     /**
  129.      * Closes the stream. This method must be called
  130.      * to release any resources associated with the
  131.      * stream.
  132.      * @exception IOException If an I/O error has occurred.
  133.      */
  134.     public native void close() throws IOException;
  135.  
  136.     /**
  137.      * Returns the file descriptor associated with this stream.
  138.      * @return the file descriptor.
  139.      */
  140.     public final int getFD() {
  141.     return fd - 1;
  142.     }
  143.  
  144.     /**
  145.      * Closes the stream when garbage is collected.
  146.      */
  147.     protected void finalize() throws IOException {
  148.     close();
  149.     }
  150. }
  151.